package com.rgp.apidriver.service.impl;

import com.rgp.apidriver.feign.ServiceDriverUserClient;
import com.rgp.apidriver.service.VerificationCodeService;
import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.common.RedisPrefixConstant;
import com.rgp.internalcommon.common.TokenDurationTimeConstant;
import com.rgp.internalcommon.common.VerificationCodeConstant;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.internalcommon.dto.UserInfoDto;
import com.rgp.internalcommon.dto.VerificationCodeDto;
import com.rgp.internalcommon.response.DriverVerificationResponse;
import com.rgp.internalcommon.response.TokenResponse;
import com.rgp.internalcommon.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-17 19:28
 */
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ServiceDriverUserClient serviceDriverUserClient;

    @Override
    public ResponseResult checkVerificationCode(VerificationCodeDto verificationCodeDto) {

        String numberCode = verificationCodeDto.getNumberCode();
        String driverUserPhone = verificationCodeDto.getUserPhone();

        //1.校验验证码【司机手机号不用校验【此前已经校验过，其手机号一定存在于db】】
        //1.1校验司机发送的验证码[是否为空，是否为指定位数]
        if (numberCode == null || !VerificationCodeConstant.VERIFICATION_CODE_SIZE.equals(numberCode.trim().length())){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATIONCODE_ERROR);
        }

        //1.2获取redis中的验证码[校验redis超时、不等]
        String redisCode = stringRedisTemplate.opsForValue().get(RedisPrefixConstant.DRIVER_VERIFICATIONCODE_PREFIX + driverUserPhone);
        if (redisCode == null){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATIONCODE_TIMEOUT_ERROR);
        }else if (!redisCode.equals(numberCode)){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATIONCODE_ERROR);
        }

        //3.获取司机用户的个人信息【姓名、电话】
        DriverVerificationResponse data = serviceDriverUserClient.getDriverByPhoneNum(driverUserPhone).getData();
        String driverName = data.getDriverName();
        String phoneNum = data.getPhoneNum();
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUserName(driverName);
        userInfoDto.setUserPhone(phoneNum);

        //4.为用户生成token【AccessToken、RefreshToken】并存入到redis
        //生成AccessToken
        String accessToken = JwtUtils.tokenGenerate(userInfoDto, TokenDurationTimeConstant.ACCESS_TOKEN_VALID_MINUTES);
        stringRedisTemplate.opsForValue().set(RedisPrefixConstant.DRIVER_ACCESSTOKEN_PREFIX + userInfoDto.getUserPhone(), accessToken,
                TokenDurationTimeConstant.ACCESS_TOKEN_VALID_MINUTES, TimeUnit.MINUTES);
        //生成RefreshToken
        String refreshToken = JwtUtils.tokenGenerate(userInfoDto, TokenDurationTimeConstant.REFRESH_TOKEN_VALID_MINUTES);
        stringRedisTemplate.opsForValue().set(RedisPrefixConstant.DRIVER_REFRESHTOKEN_PREFIX + userInfoDto.getUserPhone(), refreshToken,
                TokenDurationTimeConstant.REFRESH_TOKEN_VALID_MINUTES, TimeUnit.MINUTES);

        //5.返回双token
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }
}